/*
 * Copyright 2012 Marcin Blazejewski
 *
 * This file is part of Ground0.
 *
 * Ground0 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ground0 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ground0. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "pilot_eye.hpp"
#include "stuff.hpp"
#include "camera.hpp"
#include "world.hpp"

#include <iostream>
#include <SDL/SDL.h>

using namespace std;

Pilot::Pilot() {
    target = NULL;
}

Pilot::~Pilot() {
}

void Pilot::set_target(Sim *t) {
    target = t;
}

void Pilot::on_keydown(SDL_KeyboardEvent *ev) {
    bool ctrl = ev->keysym.mod & KMOD_CTRL;
    bool shift = ev->keysym.mod & KMOD_SHIFT;
    
    if (!ctrl && !shift) {
        switch (ev->keysym.sym) {
        case SDLK_LEFT:  target->act[2] = -1; target->act[1] = 1; break;
        case SDLK_RIGHT: target->act[2] =  1; target->act[1] = -1; break;
        case SDLK_UP:    target->act[0] =  1; break;
        case SDLK_DOWN:  target->act[0] = -1; break;
        case SDLK_s:     target->act[1] =  2; break;
        case SDLK_d:     target->act[1] = -2; break;
        case SDLK_a:     target->thrust.z =  1; break;
        case SDLK_z:     target->thrust.z = -1; break;
        case SDLK_SPACE: target->fire = true; break;
        }
    }
    else if (!ctrl && shift) {
        switch (ev->keysym.sym) {
        case SDLK_LEFT:  target->thrust.x = -0.3; break;
        case SDLK_RIGHT: target->thrust.x =  0.3; break;
        case SDLK_UP:    target->thrust.z =  0.3; break;
        case SDLK_DOWN:  target->thrust.z = -0.3; break;
        }
    }

}

void Pilot::on_keyup(SDL_KeyboardEvent *ev) {

    bool ctrl = ev->keysym.mod & KMOD_CTRL;
    bool shift = ev->keysym.mod & KMOD_SHIFT;
    
    if (!ctrl && !shift) {
        switch (ev->keysym.sym) {
        case SDLK_LEFT:  target->act[2] =  0; target->act[1] = 0; break;
        case SDLK_RIGHT: target->act[2] =  0; target->act[1] = 0; break;
        case SDLK_UP:    target->act[0] =  0; break;
        case SDLK_DOWN:  target->act[0] =  0; break;
        case SDLK_s:     target->act[1] =  0; break;
        case SDLK_d:     target->act[1] =  0; break;
        case SDLK_a:     target->thrust.z =  0; break;
        case SDLK_z:     target->thrust.z =  0; break;
        case SDLK_i: target->velocity.y += 1; break;
        case SDLK_k: target->velocity.y -= 1; break;
        case SDLK_j: target->velocity.x += 1; break;
        case SDLK_l: target->velocity.x -= 1; break;
        case SDLK_y: target->velocity.z += 1; break;
        case SDLK_h: target->velocity.z -= 1; break;
        case SDLK_p: target->velocity *= 0; break;
        case SDLK_TAB:   
            if (--target->auto_heading < 0) target->auto_heading = AUTO_HEADING_LEVELS;
            if (target->auto_heading) target->auto_speed = target->velocity.length();
            break;
        case SDLK_SPACE: target->fire = false; break;
            
        }
    }
    else if (!ctrl && shift) {
    }

    target->thrust.x = 0;
    target->thrust.z = 0;
}


/*
void __rm_render() {
    // render scene
    scene->render();

    // target marker
    World *w = (World*)scene;
    Stuff *t = w->get_next_gate();
    Vector d = t->pos - camera->get_pos();

    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    glColor3f(0,1,0);

    if (d.length() > 200) {
        d.normalize();
        d *= 50;
        d += camera->get_pos();
        glPushMatrix();
        glTranslatef(d.x, d.y, d.z);
        //glCallList(cr_disp_list);
        glCallList(marker_dl);
        glPopMatrix();
    }
    else {
        //glPushMatrix();
        //target->draw_sel();
        //glPopMatrix();
        
        glPushMatrix();
        t->draw_sel();
        glPopMatrix();
    }

    // Text
    glEnable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    
    glColor4f(1.0, 1.0, 1.0, 1.0);

    char s[100];
    sprintf(s, "speed: %d", (int)target->velocity.length());
    print(0, 0, s);
    if (target->auto_heading) {
        sprintf(s, "[AUTO-H%.0f]", target->auto_heading);
        print(0, 20, s);
    }

}
*/

/* EOF */
